
package model;

import java.util.ArrayList;


public class Lap {
	private float min_vel;
	private float max_vel;
	private float avg_vel;
	private Temps time;
	private short numLap;
	private long lenght;
	private ArrayList<GGA> ggas; //TODO obtenir ggas que pertanyen a la lap
	private ArrayList<RMC> rmcs; //TODO obtenir rmcs que pertanyen a la lap
	private int[] rgb;
	private boolean init;
	private boolean mostrar = true;

	public Lap() {
		init = false;
		ggas = new ArrayList<GGA>();
		rmcs = new ArrayList<RMC>();
	}

	private void calcularValors() {
		int suma = 0;
		Temps total = new Temps();
		lenght = 0;
		for (int i = 0; i < rmcs.size(); i++) {
			if (i == 0) {
				min_vel = rmcs.get(i).getSpeed();
				max_vel = rmcs.get(i).getSpeed();
			} else {
				lenght += calcularDistancia(rmcs.get(i-1), rmcs.get(i));
				total.afegir(restarTemps(rmcs.get(i-1).getTime(), rmcs.get(i).getTime()));

				if (rmcs.get(i).getSpeed() > max_vel) {
					max_vel = rmcs.get(i).getSpeed();
				}
				if (rmcs.get(i).getSpeed() < min_vel) {
					min_vel = rmcs.get(i).getSpeed();
				}
			}
			suma += rmcs.get(i).getSpeed();
		}
		avg_vel = suma/rmcs.size();
		init = true;
		setTime(total);
	}

	private int calcularDistancia(RMC rmc, RMC rmc2) {
		Temps t = new Temps(rmc2.getTime());
		t.restar(rmc.getTime());
		float speed = rmc2.getSpeed();
		return (int)(speed*t.getValor());
	}

	private Temps restarTemps(Temps t1, Temps t2) {
		Temps result = new Temps(t2);
		result.restar(t1);
		return result;
	}

	public float getMin_vel() {
		if (!init) calcularValors();
		return min_vel;
	}
	public void setMin_vel(float min_vel) {
		this.min_vel = min_vel;
	}
	public float getMax_vel() {
		if (!init) calcularValors();
		return max_vel;
	}
	public void setMax_vel(float max_vel) {
		this.max_vel = max_vel;
	}
	public float getAvg_vel() {
		if (!init) calcularValors();
		return avg_vel;
	}
	public void setAvg_vel(float avg_vel) {
		this.avg_vel = avg_vel;
	}
	public Temps getTime() {
		if (!init) calcularValors();
		return time;
	}
	public void setTime(Temps time) {
		this.time = time;
	}
	public short getLap() {
		if (!init) calcularValors();
		return numLap;
	}
	public void setLap(short lap) {
		this.numLap = lap;
	}
	public long getLenght() {
		if (!init) calcularValors();
		return lenght;
	}
	public void setLenght(long lenght) {
		this.lenght = lenght;
	}
	public short getNumLap() {
		if (!init) calcularValors();
		return numLap;
	}
	public void setNumLap(short numLap) {
		this.numLap = numLap;
	}
	public ArrayList<GGA> getGgas() {
		return ggas;
	}
	public void setGgas(ArrayList<GGA> ggas) {
		this.ggas = ggas;
	}

	public ArrayList<RMC> getRmcs() {
		return rmcs;
	}
	public void setRmcs(ArrayList<RMC> rmcs) {
		this.rmcs = rmcs;
		init = false;;
	}

	public int[] getRgb() {
		return rgb;
	}

	public void setRgb(int r, int g, int b) {
		this.rgb = new int[3];
		rgb[0] = r;
		rgb[1] = g;
		rgb[2] = b;
	}

	public boolean isMostrar() {
		return mostrar;
	}

	public void setMostrar(boolean mostrar) {
		this.mostrar = mostrar;
	}
	
	
}
